var util = require('../../../../../utils/util.js');
var api = require('../../../../../config/api.js');
const draw = require('../../../../../utils/common/drawhaibao.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    twoCodeUrl: "",
    template: {},
    image: "",
    info: {},
    avatarUrl: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getinfo();
    wx.showLoading({
      title: '正在努力生成中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000);
    var scene = {
      "page": "home",
      "page_data_id": ""
    };
    util.request(api.CreateQcodeImg, {
      scene: JSON.stringify(scene)
    }, 'GET').then(function (res) {
      that.setData({
        twoCodeUrl: res.data
      });
      wx.getSetting({
        success: function (res) {
          //不存在相册授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: function () {
                var drawtemplate = draw.getfenxiaoDraw(that.data.info.dianpuzhaopian, that.data.info.dianpuname, that.data.info.avatar, that.data.info.nickname, that.data.info.jianjie, that.data.twoCodeUrl);
                that.setData({
                  template: drawtemplate
                });
              },
              fail: function (err) {
                // 授权失败
                wx.showModal({
                  title: '警告',
                  content: '您点击了拒绝授权,将无法正常保存图片,点击确定重新获取授权。',
                  success: function (res) {
                    if (res.confirm) {
                      wx.openSetting({
                        success: (res) => {
                          console.log('授权成功')
                        }
                      })
                    }
                  }
                })
              }
            })
          } else {
            var drawtemplate = draw.getfenxiaoDraw(that.data.info.dianpuzhaopian, that.data.info.dianpuname, that.data.info.avatar, that.data.info.nickname, that.data.info.jianjie, that.data.twoCodeUrl);
            that.setData({
              template: drawtemplate
            });
          }
        }
      })
    });
  },
  //获取信息
  getinfo: function () {
    let that = this;
    util.request(api.Fenxiaotuiguang).then(function (res) {
      if (res.result == 1) {
        that.setData({
          info: res.data
        });
        wx.getImageInfo({
          src: res.data.avatar,
          success: function (res) {
            that.setData({
              avatarUrl: res.path
            });
          }
        })
      }
    });
  },

  //保存海报
  savehaibao: function () {
    let that = this;
    wx.getSetting({
      success: (set) => {
        wx.saveImageToPhotosAlbum({
          filePath: that.data.image,
          success: (res) => {
            if (res.errMsg == "saveImageToPhotosAlbum:ok") {
              wx.showModal({
                title: '存图成功',
                content: '图片成功保存到相册了，可以分享到朋友圈了',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#a78845',
                success: function (res) {
                  if (res.confirm) {}
                }
              })
            }
          }
        })
        if (set.authSetting['scope.writePhotosAlbum'] == false) {
          wx.openSetting()
        }
      }
    })
  },


  previewImg() {
    wx.previewImage({
      current: this.data.image,
      urls: [this.data.image]
    })
  },

  // 生成成功
  canvasSuc(e) {
    wx.hideLoading()
    this.setData({
      image: e.detail.path
    })
  },

  //预览图片
  previewImg() {
    wx.previewImage({
      current: this.data.image,
      urls: [this.data.image]
    })
  },

  //保存海报
  savehaibao: function () {
    let that = this;
    wx.getSetting({
      success: (set) => {
        wx.saveImageToPhotosAlbum({
          filePath: that.data.image,
          success: (res) => {
            if (res.errMsg == "saveImageToPhotosAlbum:ok") {
              wx.showModal({
                title: '存图成功',
                content: '图片成功保存到相册了，可以分享到朋友圈了',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#a78845',
                success: function (res) {
                  if (res.confirm) {}
                }
              })
            }
          }
        })
        if (set.authSetting['scope.writePhotosAlbum'] == false) {
          wx.openSetting()
        }
      }
    })
  },

  closetwocodeimg: function () {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})